package cn.zdq.demo_net;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.List;

import cn.zdq.demo_net.api.WanAndroidApi;
import cn.zdq.demo_net.bean.BannerBean;
import cn.zdq.lib_net.RxHttpUtils;
import cn.zdq.lib_net.interceptor.Transformer;
import cn.zdq.lib_net.interfaces.ILoadingView;
import cn.zdq.lib_net.observer.DataObserver;

import static cn.zdq.lib_net.utils.ToastUtils.showToast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private Button download_http, download_cancel_http;
    private ILoadingView loading_dialog;
    private TextView responseTv;

    private int REQUEST_CODE_CHOOSE = 1;
    private String UPLOAD_URL = "http://t.xinhuo.com/index.php/Api/Pic/uploadPic";
    private RxPermissions permissions;
    private int MAX_SELECTABLE = 1;
    private boolean IS_USE_GLOBAL_CONFIG = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        responseTv = (TextView) findViewById(R.id.response_tv);
        findViewById(R.id.douan_api_http).setOnClickListener(this);
        findViewById(R.id.other_open_api_http).setOnClickListener(this);
        findViewById(R.id.wanandroid_api_http).setOnClickListener(this);
        findViewById(R.id.global_http).setOnClickListener(this);
        findViewById(R.id.multiple_http).setOnClickListener(this);
        download_http = (Button) findViewById(R.id.download_http);
        download_cancel_http = (Button) findViewById(R.id.download_cancel_http);
        findViewById(R.id.download_http).setOnClickListener(this);
        findViewById(R.id.download_cancel_http).setOnClickListener(this);
        findViewById(R.id.upload_one_pic).setOnClickListener(this);
        findViewById(R.id.upload_more_pic).setOnClickListener(this);
        findViewById(R.id.upload_pic_with_global_config).setOnClickListener(this);
        findViewById(R.id.global_string_http).setOnClickListener(this);

        permissions = new RxPermissions(this);
    }

    @Override
    public void onClick(View v) {
        responseTv.setText("");
        switch (v.getId()) {
            case R.id.global_http:
                RxHttpUtils.createApi(WanAndroidApi.class)
                        .getBanner()
                        .compose(Transformer.switchSchedulers())
                        .subscribe(new DataObserver<List<BannerBean>>() {
                            @Override
                            protected void onError(String errorMsg) {
                                showToast(errorMsg);
                            }

                            @Override
                            protected void onSuccess(List<BannerBean> data) {
                                String s = data.get(0).toString();
                                responseTv.setText(s);
                                showToast(s);
                            }
                        });
        }
    }
}